package cn.com.scitc.licenseplateback.identityVerification;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class IdentityVerification {

    public Boolean identityVerification(String cardNo, String carlName) {
        String host = "https://eid.shumaidata.com";
        String path = "/eid/check";
        String method = "POST";
        String appcode = "50fdcc63fca446cb95711dfd2359cf51";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("idcard", cardNo);
        querys.put("name", carlName);
        Map<String, String> bodys = new HashMap<String, String>();

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//            System.out.println("info => "+response.toString());
//
//            //获取response的body
//            System.out.println("-----------------------------------");
//            System.out.println(EntityUtils.toString(response.getEntity()));
//            System.out.println("-----------------------------------");

            //获取response的body
            String responceBody = EntityUtils.toString(response.getEntity());

            //将结果 JSON 字符类型的数据 转换 为 JSON 对象
            JSONObject jsonBody = JSONObject.parseObject(responceBody);
            JSONObject infoBody = JSONObject.parseObject(jsonBody.get("result").toString());

            if (infoBody.get("res").toString().equals("1")){
                return true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }


//    @Test
//    void test(){
//        //{"code":"0","message":"成功","result":{"name":"张健强","idcard":"511521199807076171","res":"1","description":"一致","sex":"男","birthday":"19980707","address":"四川省宜宾县"}}
//        // String str = {"code":"0","message":"成功","result":{"name":"张健强","idcard":"511521199807076172","res":"3","description":"服务结果:库中无此号，请到户籍所在地进行核实","sex":"男","birthday":"19980707","address":"四川省宜宾县"}}
//         String str = "{\"code\":\"0\",\"message\":\"成功\",\"result\":{\"name\":\"张健强\",\"idcard\":\"511521199807076172\",\"res\":\"3\",\"description\":\"服务结果:库中无此号，请到户籍所在地进行核实\",\"sex\":\"男\",\"birthday\":\"19980707\",\"address\":\"四川省宜宾县\"}}";
////        String str = "{\"code\":\"0\",\"message\":\"成功\",\"result\":{\"name\":\"张健强\",\"idcard\":\"511521199807076171\",\"res\":\"1\",\"description\":\"一致\",\"sex\":\"男\",\"birthday\":\"19980707\",\"address\":\"四川省宜宾县\"}}";
//        JSONObject jsonObject = JSONObject.parseObject(str);
//
//        System.out.println(jsonObject.get("message"));
//        System.out.println(jsonObject.get("result"));
//
//        JSONObject info = JSONObject.parseObject(jsonObject.get("result").toString());
//        System.out.println(info.get("res"));
//        System.out.println(info.get("description"));
//    }
}
